/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uvexposureapplication.eventrouting.elvin;

import java.io.IOException;
import java.net.ConnectException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.avis.client.*;
import org.avis.common.InvalidURIException;
import uvexposureapplication.eventrouting.Producer;

/**
 *
 * @author matthieukluj
 */
public class ElvinProducer implements Producer {

    private Elvin elvin;

    public ElvinProducer(String server) {
        this.connectProducer(server);
    }

    public void produce(String parameter, String content) {

        Notification not;

        not = new Notification();
        not.set(parameter, content);
        try {
            elvin.send(not); //send the notification
        } catch (IOException ex) {
            Logger.getLogger(ElvinProducer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void produceFromTable(String[][] parameters) {

        Notification not;

        not = new Notification();

        int i = 0;
        while (i <= parameters.length) {
            not.set(parameters[i][0], parameters[i][1]);
        }
        try {
            elvin.send(not); //send the notification
        } catch (IOException ex) {
            Logger.getLogger(ElvinProducer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void produceFromCollection(Map<String, String> parameters) {

        Notification not;

        not = new Notification();

        for (Map.Entry<String, String> entry : parameters.entrySet()) {
            not.set(entry.getKey(), entry.getValue());
        }
        try {
            elvin.send(not); //send the notification
        } catch (IOException ex) {
            Logger.getLogger(ElvinProducer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void connectProducer(String server) {
        try {
            elvin = new Elvin(server);
        } catch (InvalidURIException ex) {
            Logger.getLogger(ElvinProducer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(ElvinProducer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ConnectException ex) {
            Logger.getLogger(ElvinProducer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ElvinProducer.class.getName()).log(Level.SEVERE, null, ex);
        }
        //System.out.println("Producer connected");
    }
    
     public void closeConnection() {
        this.elvin.close();
    }
}
